<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
          "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="${packageName}.mapper.${className}Mapper">
<!-- ******************************************** 通用map配置开始 ************************************************ -->
	<!--设置domain类和数据库中表的字段一一对应，注意数据库字段和domain类中的字段名称不致，此处一定要！ -->
	<!--实体映射 -->
	<resultMap id="base_result_map" type="${packageName}.pojo.${className}">
			<!--ID -->
			<id property="id" column="id" />
	 <#list columnInfoByCon as var>
	    <#if var.columnName == "id">
		  <#else> 
			<result property="${var.attrName}" column="${var.columnName}" />
	    </#if>
	 </#list>
	</resultMap>

	<!-- 通用查询结果列 -->
	<sql id="base_column_list">
	  <#list columnInfoByCon as var>
	    <#if var.columnName == "id">
		  <#else> 
		${var.columnName},
	   </#if> 
	  </#list>
	    id
	</sql>

	<!-- 通用条件判断 -->
	<sql id="base_condition_filed">
		    <if test="id != null and id !=''">
				and id = ${r"#{id}"}
			</if>
	  <#list columnInfoByCon as var>
	    <#if var.columnName == "id">
		  <#else> 
			<if test="${var.attrName} != null and ${var.attrName} !=''">
				and ${var.columnName} = ${r"#{"}${var.attrName}${r"}"}
			</if>
	    </#if>
	  </#list>
	</sql>

	<!-- 通用条件列 -->
	<sql id="base_condition">
		<where>
			<include refid="base_condition_filed" />
		</where>
	</sql>

	<!-- 通用查询条件列 -->
	<sql id="query_condition">
		<where>
			<include refid="base_condition_filed" />
		  <#list columnInfoByCon as var>
			<if test="${var.attrName}Fuzzy != null and ${var.attrName}Fuzzy !=''">
				and ${var.columnName} like concat('%', ${r"#{"}${var.attrName}Fuzzy${r"}"}, '%')
			</if>
		  </#list>
		</where>
	</sql>

<!-- ******************************************** 通用map配置结束 ************************************************ -->
	<!-- 查询数量 -->
	<select id="selectListCount" resultType="java.lang.Integer" >
		SELECT count(1) FROM ${tableName}
		<include refid="base_condition" />
	</select>

	<!-- 查询集合 -->
	<select id="selectList" resultType="pd">
		SELECT
		<include refid="base_column_list" />
		FROM ${tableName}
		<include refid="base_condition" />
		<if test="orderBy!=null">
			order by ${r"${orderBy}"}
		</if>
		<if test="page!=null">
			limit ${r"#{page.start}"},${r"#{page.end}"}
			
		</if>
	</select>

    <!-- 根据PrimaryKey获取对象 -->
	<select id="getById" resultType="pd">
		select
		<include refid="base_column_list" />
		from ${tableName} where id = ${r"#{id}"}
	</select>
	
	<!-- 插入 （匹配有值的字段） -->
	<insert id="add" parameterType="pd">
		INSERT INTO ${tableName}
		<trim prefix="(" suffix=")" suffixOverrides=",">
		  <#list columnInfoByCon as var>
			<if test="${var.attrName} != null and ${var.attrName} !='' ">
				${var.columnName},
			</if>
		  </#list>
		</trim>
		<trim prefix="values (" suffix=")" suffixOverrides=",">
		  <#list columnInfoByCon as var>
			<if test="${var.attrName} != null and ${var.attrName} !='' ">
				${r"#{"}${var.attrName}${r"}"},
			</if>
		  </#list>
		</trim>
	</insert>


    <!-- 根据PrimaryKey修 改 -->
	<update id="updateById" parameterType="pd">
		UPDATE ${tableName}
		<set>
		  <#list columnInfoByCon as var>
			<if test="${var.attrName} != null and ${var.attrName} !='' ">
				${var.columnName} = ${r"#{"}${var.attrName}${r"}"},
			</if>
		  </#list>
		</set>
		where id = ${r"#{id}"}
	</update>

    <!-- 根据PrimaryKey删除 -->
	<delete id="deleteById">
		delete from ${tableName} where id = ${r"#{id}"}
	</delete>

</mapper>